package com.example.active.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Receiver implements Runnable {

    public String url;
    public String queue ;

    public Receiver(String url, String queue) {
        this.url = url;
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queue);
            MessageConsumer consumer = session.createConsumer(destination);
            while (true) {
                TextMessage messageIn = (TextMessage) consumer.receive(100000);
                if (messageIn != null) {
                    System.out.println("来自对方:" + messageIn.getText());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
